using Microsoft.SqlServer.Management.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Microsoft.SqlServer.Management.SqlManagerUI
{
	internal class PickObjectTypesWizardPage : WizardPage
	{
		private Button selectAllButton;

		private Button clearAllButton;

		private ListView objectListBox;

		private ColumnHeader columnHeader1;

		private GenScriptWizForm parent;

		public PickObjectTypesWizardPage(WizardForm wizard) : base(wizard)
		{
			this.InitializeComponent();
			this.objectListBox.Columns[0].Width = this.objectListBox.Width;
			this.objectListBox.Text = GenScriptWizSR.GenScriptObjectTypeSqlObjTypes;
			this.selectAllButton.Text = GenScriptWizSR.GenScriptObjectTypeSelBtnText;
			this.selectAllButton.AccessibleName = GenScriptWizSR.MSAAGenObjectTypeFormSelBtnText;
			this.clearAllButton.Text = GenScriptWizSR.GenScriptObjectTypeClearBtnText;
			this.clearAllButton.AccessibleName = GenScriptWizSR.MSAAGenObjectTypeFormClearBtnText;
			this.SubTitle = GenScriptWizSR.GenScriptObjectTypeSubtitle;
			this.Title = GenScriptWizSR.GenScriptObjectTypeTitle;
			base.HelpTopicKeyword = "sql9.swb.generatescriptswizard.chooseobjecttypes.f1";
			this.objectListBox.AccessibleName = GenScriptWizSR.MSAAGenScriptObjectTypeTitle;
			this.parent = (GenScriptWizForm)wizard;
			base.PageID = 2;
		}

		private void LoadAvailableObjectTypes(List<GenWizObjectType> allObjectTypes)
		{
			GenWizGeneralData genWizGeneralData = (GenWizGeneralData)base.WizardInputs.FindItem(0);
			foreach (GenWizObjectType current in allObjectTypes)
			{
				bool flag;
				genWizGeneralData.GetObjectsList(current.PageId, true, out flag);
				if (flag)
				{
					ListViewItem listViewItem = this.objectListBox.Items.Add(current.FriendlyName);
					listViewItem.Tag = current;
				}
			}
		}

		protected override void OnInitializePage(EventArgs e)
		{
			this.Wizard.BackEnabled = true;
			List<GenWizObjectType> allObjectTypes = (List<GenWizObjectType>)base.WizardInputs.FindItem(2);
			this.LoadAvailableObjectTypes(allObjectTypes);
		}

		protected override void OnEnterPage(EventArgs e)
		{
			this.CheckButtonState();
		}

		private void SelectAllClick(object sender, EventArgs e)
		{
			this.Cursor = Cursors.WaitCursor;
			foreach (ListViewItem listViewItem in this.objectListBox.Items)
			{
				listViewItem.Checked = true;
			}
			this.CheckButtonState();
			this.Cursor = Cursors.Default;
		}

		private void ClearAllClick(object sender, EventArgs e)
		{
			this.Cursor = Cursors.WaitCursor;
			foreach (ListViewItem listViewItem in this.objectListBox.Items)
			{
				listViewItem.Checked = false;
			}
			this.CheckButtonState();
			this.Cursor = Cursors.Default;
		}

		private void CheckButtonState()
		{
			bool flag = this.AnyChecked();
			this.selectAllButton.Enabled = !this.AllChecked();
			this.clearAllButton.Enabled = flag;
			this.Wizard.NextEnabled = flag;
			base.FinishEnabled = this.parent.CanEnableFinish();
		}

		private bool AnyChecked()
		{
			return this.objectListBox.CheckedIndices.Count > 0;
		}

		private bool AllChecked()
		{
			return this.objectListBox.CheckedIndices.Count == this.objectListBox.Items.Count;
		}

		public void AddRemovePages()
		{
			foreach (ListViewItem listViewItem in this.objectListBox.Items)
			{
				GenWizObjectType genWizObjectType = (GenWizObjectType)listViewItem.Tag;
				if (listViewItem.Checked)
				{
					this.parent.AddPage(new GenScriptSelectForm(this.parent, genWizObjectType), (int)genWizObjectType.PageId, 1);
					genWizObjectType.IsSelected = true;
				}
				else
				{
					this.parent.RemovePage((int)genWizObjectType.PageId);
					genWizObjectType.IsSelected = false;
				}
			}
		}

		private void InitializeComponent()
		{
			this.selectAllButton = new Button();
			this.clearAllButton = new Button();
			this.objectListBox = new ListView();
			this.columnHeader1 = new ColumnHeader();
			base.SuspendLayout();
			this.selectAllButton.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.selectAllButton.Enabled = false;
			this.selectAllButton.Location = new Point(16, 210);
			this.selectAllButton.Name = "selectAllButton";
			this.selectAllButton.Size = new Size(108, 23);
			this.selectAllButton.TabIndex = 7;
			this.selectAllButton.Click += new EventHandler(this.SelectAllClick);
			this.clearAllButton.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.clearAllButton.Enabled = false;
			this.clearAllButton.Location = new Point(130, 210);
			this.clearAllButton.Name = "clearAllButton";
			this.clearAllButton.Size = new Size(108, 23);
			this.clearAllButton.TabIndex = 8;
			this.clearAllButton.Click += new EventHandler(this.ClearAllClick);
			this.objectListBox.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.objectListBox.CheckBoxes = true;
			this.objectListBox.Columns.AddRange(new ColumnHeader[]
			{
				this.columnHeader1
			});
			this.objectListBox.FullRowSelect = true;
			this.objectListBox.HeaderStyle = ColumnHeaderStyle.None;
			this.objectListBox.Location = new Point(16, 8);
			this.objectListBox.Name = "objectListBox";
			this.objectListBox.Size = new Size(432, 191);
			this.objectListBox.TabIndex = 9;
			this.objectListBox.View = View.Details;
			this.objectListBox.ItemChecked += new ItemCheckedEventHandler(this.objectListBox_ItemChecked);
			this.objectListBox.Resize += new EventHandler(this.objectListBox_Resize);
			base.Controls.Add(this.objectListBox);
			base.Controls.Add(this.selectAllButton);
			base.Controls.Add(this.clearAllButton);
			base.Name = "PickObjectTypesWizardPage";
			base.Size = new Size(464, 245);
			base.ResumeLayout(false);
		}

		private void objectListBox_ItemChecked(object sender, ItemCheckedEventArgs e)
		{
			GenWizObjectType genWizObjectType = (GenWizObjectType)e.Item.Tag;
			if (genWizObjectType != null)
			{
				genWizObjectType.IsSelected = e.Item.Checked;
			}
			this.CheckButtonState();
		}

		private void objectListBox_Resize(object sender, EventArgs e)
		{
			this.objectListBox.Columns[0].Width = this.objectListBox.Width;
		}
	}
}
